#ifdef __APPLE__
#include <OpenGL/OpenGL.h>
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

#include <iostream>
#include <stdlib.h>
#include <cstdlib>
#include <math.h>
#include <vector>

#include "camera.h"
#include "color.h"
#include "matrix.h"

using namespace std;


GLuint teapotList;

void initTea(){
	
	teapotList = glGenLists(1);
	glNewList (teapotList, GL_COMPILE);
	glutSolidTeapot(1.0);
	glEndList ();
}

void shear(){
	Matrix s;
	s.setToIdentity();
	s.set(2,0,2);
//	s.print();
	glMultMatrixd(s.fixmatrix());

}
void draw(){
	GLUquadricObj *quadratic;
	quadratic=gluNewQuadric();
	gluQuadricNormals(quadratic, GLU_SMOOTH);
	gluQuadricTexture(quadratic, GL_TRUE);
	gluSphere(quadratic,5.3f,32,32);
	GLUquadricObj* quad2;
	quad2 = gluNewQuadric();
	glPushMatrix();
	glTranslatef( 0.0f,6.0f, 4.0f);
	gluCylinder(quad2, 3, 1, 4, 32, 32);
	glPopMatrix();

}


void renderTeapot(GLfloat x, GLfloat y, 
	GLfloat ambr, GLfloat ambg, GLfloat ambb,
	GLfloat difr, GLfloat difg, GLfloat difb,
	GLfloat specr, GLfloat specg, GLfloat specb, GLfloat shine)
{
   GLfloat mat[4];

   glPushMatrix();
   glTranslatef(x, y, 0.0);
   mat[0] = ambr; mat[1] = ambg; mat[2] = ambb; mat[3] = 1.0;
   glMaterialfv(GL_FRONT, GL_AMBIENT, mat);
   mat[0] = difr; mat[1] = difg; mat[2] = difb;
   glMaterialfv(GL_FRONT, GL_DIFFUSE, mat);
   mat[0] = specr; mat[1] = specg; mat[2] = specb;
   glMaterialfv(GL_FRONT, GL_SPECULAR, mat);
   glMaterialf(GL_FRONT, GL_SHININESS, shine * 128.0);
   glCallList(teapotList);
   glPopMatrix();
}

void teatime(void){
	glPushMatrix();

	glColor3f(1.0f, 0.0f, 0.0f);
	glFrontFace(GL_CW);

	//put the mesh bit
	if(trans != 0 && mesh){
		glPushMatrix();
		glRotatef(90, 1.0f, 0.0f, 0.0f);
		glTranslatef(8.0f, 4.0f, 0.0f);
		glScalef(4.0f, 4.0f, 4.0f);
		glutWireTeapot(1.0);
		glPopMatrix();
	}

	//pick a transformation
	switch (trans) {
		case 0: //do nothing
			break;
		case 1:
			
			glTranslatef(8.0f, 3.0f, 12.0f);
			break;
		case 2: 
			glScalef(3.0f, 0.5f, 1.5f);
			break;		
		case 3:
			glRotatef(15, 0.0f, 1.0f, 0.0f); 
			glRotatef(15, 1.0f, 0.0f, 0.0f); 
			glRotatef(180, 0.0f, 0.0f, 1.0f);
			break;		
		case 4:
			shear();
			break;
	}
	//Put the teapot in the right place
	glRotatef(90, 1.0f, 0.0f, 0.0f);
	glTranslatef(8.0f, 4.0f, 0.0f);
	glScalef(4.0f, 4.0f, 4.0f);
	renderTeapot(0.0, 0.0, 0.0215, 0.1745, 0.0215, 0.07568, 0.61424, 0.07568, 0.633, 0.727811, 0.633, 0.6);
	glPopMatrix();
	glFrontFace(GL_CCW);
}


